package com.tbit.main.service.impl;

import com.tbit.main.constant.UserConstant;
import com.tbit.main.dao.core.AccountDao;
import com.tbit.main.pojo.Account;
import com.tbit.main.service.PurviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 权限检查
 *
 */
@Service
public class PurviewServiceImpl implements PurviewService {

    @Autowired
    private AccountDao accountDao;
    @Override
    public boolean purviewCheck(Integer operateId, Integer objId) {
        Account opAcc = accountDao.getByAccountId(operateId);
        Account objAcc = accountDao.getByAccountId(objId);
        return purviewCheck(opAcc, objAcc);
    }

    @Override
    public boolean purviewCheck(Account opAcc, Account objAcc) {
        if (opAcc == null) {
            return false;
        } else {
            if (null == objAcc) {
                return true;
            } else {
                Integer operateType = opAcc.getType();
                Integer objType = objAcc.getType();
                Integer operateId = opAcc.getAccountId();
                Integer objId = objAcc.getAccountId();
                if (Objects.equals(operateType, objType)) {
                    if (Objects.equals(operateId, objId)) {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    if (Objects.equals(operateType, UserConstant.ACCOUNT_ADMIN)) {
                        return true;
                    } else {
                        Map<String, Object> map = new HashMap<>();
                        if (Objects.equals(opAcc.getType(), UserConstant.ACCOUNT_ADMIN)) {
                            // 不加过滤
                        } else if (Objects.equals(opAcc.getType(), UserConstant.ACCOUNT_BRAND)) {
                            // 加入pid过滤
                            map.put("bid", opAcc.getAccountId());
                        } else if (Objects.equals(opAcc.getType(), UserConstant.ACCOUNT_FRANCHISEE)) {
                            // 加入fid过滤
                            map.put("fid", opAcc.getAccountId());
                        } else if (Objects.equals(opAcc.getType(), UserConstant.ACCOUNT_AGENT)) {
                            // 加入aid过滤
                            map.put("aid", opAcc.getAccountId());
                        } else {
                            return false;
                        }
                        List<Account> accountList = accountDao.getAccountTree(map);
                        for (Account item : accountList) {
                            if (Objects.equals(item.getAccountId(), objId)) {
                                return true;
                            }
                        }
                    }
                    return false;
                }
            }
        }
    }
}
